#include <QtOpenGL>
#include "cubo.h"
#include <GL/glu.h>

bool isCubo = false;
bool MostraShader = false;
//MainWindow *MW;

Cubo::Cubo(MainWindow *mainWindow, const QVector3D& center, QObject *parent):
    Object3D(center, parent)
{
     QDir d("../DojoGL/");
        program.addShaderFromSourceFile(QGLShader::Vertex,   d.absolutePath()+"/cubotoball.vert");
        program.addShaderFromSourceFile(QGLShader::Fragment, d.absolutePath()+"/cubotoball.frag");
        program.link();

     connect(mainWindow, SIGNAL(radioEvent(bool,bool)),
             this, SLOT(FiguraSelecionada(bool,bool)));

     connect(mainWindow, SIGNAL(colorchoose(int)), this, SLOT(RecebeCor(int)));

     connect(mainWindow, SIGNAL(ShaderSelect(bool)), this, SLOT(ShaderLigado(bool)));

}

//void Cubo::RecebeMainWindow(MainWindow *mainWindow){
//MW = mainWindow;
//}
void Cubo::ShaderLigado(bool ligado){

    MostraShader = ligado;


}
void Cubo::RecebeCor(int cor){
    if(cor == 0 && isCubo){
        glColor3f(1.0,0.0,0.0);
    }
    else if(cor == 1 && isCubo){glColor3f(0.0,1.0,0.0);}
    else if(cor == 2 && isCubo){glColor3f(0.0,0.0,1.0);}
    //updateGL();
    //else {glColor3f(1,1,1);}

}

void Cubo::FiguraSelecionada(bool obj1,bool obj2){
    if(obj1){
   isCubo = false;
    }
    if(obj2){
    isCubo = true;
    }
}

void Cubo::drawGeometry(void)
{
    if(MostraShader) { program.bind();}
    glScalef(0.5,0.5,0.5);
    glBegin(GL_QUADS);


       // glColor3f(1.0,0.0,0.0);
        glNormal3f(0.0,0.0,-1.0);
        glVertex3i(-1,-1,-1);
        glVertex3i(-1,1,-1);
        glVertex3i(1,1,-1);
        glVertex3i(1,-1,-1);

       // glColor3f(0.0,1.0,0.0);
        glNormal3f(0.0,0.0,1.0);
        glVertex3i(-1,-1,1);
        glVertex3i(1,-1,1);
        glVertex3i(1,1,1);
        glVertex3i(-1,1,1);

       // glColor3f(0.0,0.0,1.0);
        glNormal3f(1.0,0.0,0.0);
        glVertex3i(1,-1,-1);
        glVertex3i(1,1,-1);
        glVertex3i(1,1,1);
        glVertex3i(1,-1,1);

        //glColor3f(0.0,1.0,1.0);
        glNormal3f(-1.0,0.0,0.0);
        glVertex3i(-1,-1,-1);
        glVertex3i(-1,-1,1);
        glVertex3i(-1,1,1);
        glVertex3i(-1,1,-1);

        //glColor3f(1.0,1.0,1.0);
        glNormal3f(0.0,1.0,0.0);
        glVertex3i(-1,1,-1);
        glVertex3i(-1,1,1);
        glVertex3i(1,1,1);
        glVertex3i(1,1,-1);

        //glColor3f(1.0,1.0,0.0);
        glNormal3f(0.0,-1.0,0.0);
        glVertex3i(-1,-1,-1);
        glVertex3i(1,-1,-1);
        glVertex3i(1,-1,1);
        glVertex3i(-1,-1,1);



        glEnd();
        if(MostraShader) {  program.release();}

}
